package com.lyhengtongwl.zqsnews.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.utils.AppConfig;
import com.lyhengtongwl.zqsnews.utils.CheckApkExist;
import com.lyhengtongwl.zqsnews.utils.CopyUtils;
import com.lyhengtongwl.zqsnews.utils.NewsToastUtils;
import com.lyhengtongwl.zqsnews.utils.PermissionUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.utils.SystemUtils;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;

import butterknife.BindView;
import butterknife.OnClick;

public class NewsContactUsActivity extends BaseActivity {

    private String wxChat;
    private String wxGG;
    private String qqGroup;
    private String qqStr;
    private String groupKey;

    @BindView(R.id.tv_qq)
    TextView tv_qq;//qq号
    @BindView(R.id.iv_qq)
    ImageView iv_qq;//会话
    @BindView(R.id.tv_wc)
    TextView tv_wc;


    @BindView(R.id.tv_group)
    TextView tv_group;//qq群

    @BindView(R.id.tv_gg)
    TextView tv_gg;//公众号

    @BindView(R.id.tv_bb)
    TextView tv_bb;//版本号
    private final String[] PERMISSIONS = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,//读取联系人权限
    };

    @OnClick({R.id.ll_copy, R.id.ll_qq, R.id.ll_group, R.id.tv_copy})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_copy://复制微信公众号
                CopyUtils.copyText(this, wxChat);
//                getPermission();
                openWX();
                break;
            case R.id.ll_qq://qq交谈
                String url3521 = "mqqwpa://im/chat?chat_type=wpa&uin=" + qqStr;
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url3521)));
                break;
            case R.id.ll_group://qq群
                if (!joinQQGroup(groupKey)) {
                    NewsToastUtils.showToast(NewsContactUsActivity.this, "未安装手Q或安装的版本不支持！");
                }
                break;

            case R.id.tv_copy://复制微信公众号
                CopyUtils.copyText(NewsContactUsActivity.this, wxGG);
//                getPermission();
                openWX();
                break;

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //回调结果
        PermissionUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    /**
     * 获取相关权限
     */
    private void getPermission() {
        if (!PermissionUtils.getInstance().checkPermission(NewsContactUsActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            PermissionUtils.getInstance().checkPermissions(this, PERMISSIONS, permissionsResult);
        } else {
            openWX();
        }
    }

    //创建监听权限的接口对象
    PermissionUtils.IPermissionsResult permissionsResult = new PermissionUtils.IPermissionsResult() {
        @Override
        public void passPermissons() {
            openWX();
        }

        @Override
        public void forbitPermissons() {
            //只要有一个被拒绝，则会走这里，拒绝不在提醒，还是会走这里
        }
    };

    //打开微信
    public void openWX() {
        if (CheckApkExist.checkApkExist(NewsContactUsActivity.this, "com.tencent.mm")) {
            Intent intent = new Intent();
            ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cmp);
            NewsContactUsActivity.this.startActivity(intent);
        } else {
            NewsToastUtils.showToast(NewsContactUsActivity.this, "未安装手微信或安装的版本不支持！");
        }
    }

    /****************
     *
     * 发起添加群流程。群号：赚钱树(818255922) 的 key 为： m871lMbeHXSE7V5_XEg5HaZQ5-9z-r-2
     * 调用 joinQQGroup(m871lMbeHXSE7V5_XEg5HaZQ5-9z-r-2) 即可发起手Q客户端申请加群 赚钱树(818255922)
     *
     * @param key 由官网生成的key
     * @return 返回true表示呼起手Q成功，返回fals表示呼起失败
     ******************/
    private boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            return false;
        }
    }

    @Override
    public void setListen() {

    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("联系我们")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_connectus;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        wxChat = AppConfig.getInstance(App.getContext()).get("wechat");
        wxGG = AppConfig.getInstance(App.getContext()).get("wxNumber");
        qqStr = AppConfig.getInstance(App.getContext()).get("qq");
        qqGroup = AppConfig.getInstance(App.getContext()).get("qqGroup");
        groupKey = AppConfig.getInstance(App.getContext()).get("groupKey");
        tv_bb.setText("：" + SystemUtils.getLocalVersionName(this));
        tv_wc.setText(wxChat);
        tv_qq.setText("3.商务合作QQ：" + qqStr);
        tv_gg.setText("1.公众号：" + wxGG);
        tv_group.setText("2.官方QQ群：" + qqGroup);
    }

    @Override
    public void initData() {

    }
}
